<?php

/************************************
 * 			GENERIC OBJECT
 ************************************/

/**
 * Create new object
 * @param $title
 * @param $description
 * @param $owner_guid
 * @param $subtype
 * @param $access_id
 * @return int|FALSE Returns object ID if success, FALSE otherwise
 * @author Pablo
 * @since 1.0
 */
function blg_object_new($title, $description, $owner_guid, $subtype, $access_id= ACCESS_DEFAULT){
	//blg_TRANSACTION();

	$time= time();
	
	//INSERT entity row
	$query= blg_dbschema_query_buid(blg_dbschema_insert_entity(array(
		'type' => 'object',
		'subtype' => $subtype,
		'owner_guid' => $owner_guid,
		'site_guid' => blg_get_config('site_guid'),
		'container_guid' => blg_get_config('site_guid'),
		'access_id' => $access_id,
		'time_created' => $time,
		'time_updated' => 0,
		'last_action' => 0,
		'enabled' => 'yes' 	
	)));
	$result= blg_dbschema_insert($query);
	$object_guid= $result->new_id;

	if($result->success){
		//INSERT object row
		$query= blg_dbschema_query_buid(blg_dbschema_insert_object(array(
			'guid' => $object_guid,
			'title' => $title,
			'description' => $description
		)));
		$result= blg_dbschema_insert($query);
		if($result->success){
			//blg_COMMIT();
			return $object_guid;
		}
	}		
	
	//blg_ROLLBACK();
	return FALSE;		
}

/**
 * Get object attributes
 * @param $guid
 * @return Array|FALSE Attributes if success, FALSE otherwise
 * @author Pablo
 * @since 1.0
 */
function blg_object_get($guid){
	$query=	blg_dbschema_query_buid(blg_dbschema_select_objects(array(
		'guid' => $guid
	)));
	$result= blg_dbschema_select($query);						
	
	if($result->num_rows==1){			
		$this->attributes= $result->{0};
	}
	
	return FALSE;
}

/**
 * Update object title
 * @param int $guid
 * @param String $title
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_object_update_title($guid, $title){
	$query= blg_dbschema_query_buid(blg_dbschema_update_objects(
		array('guid' => $guid),
		array('title' => $title)
	));
	$result= blg_dbschema_update($query);
	return $result->success;
}

/**
 * Update object description
 * @param int $guid
 * @param String $description
 * @return Boolean
 * @author Pablo
 * @since 1.0
 */
function blg_object_update_description($guid, $description){
	$query= blg_dbschema_query_buid(blg_dbschema_update_objects(
		array('guid' => $guid),
		array('description' => $description)
	));
	$result= blg_dbschema_update($query);
	return $result->success;	
}
